<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120">
      <el-form-item label="认证用户" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入认证用户" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="认证的身份Id" prop="postId">
        <el-input v-model="queryParams.postId" placeholder="请输入认证的身份Id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="所属报部门" prop="deptId">
        <el-input v-model="queryParams.deptId" placeholder="请输入所属报部门" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in business_works_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="不通过原因" prop="reason">
        <el-input v-model="queryParams.reason" placeholder="请输入不通过原因" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="申请原因" prop="applicationReason">
        <el-input v-model="queryParams.applicationReason" placeholder="请输入申请原因" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--            type="primary"-->
      <!--            plain-->
      <!--            icon="Plus"-->
      <!--            @click="handleAdd"-->
      <!--            v-hasPermi="['business:authRecord:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--            type="success"-->
      <!--            plain-->
      <!--            icon="Edit"-->
      <!--            :disabled="single"-->
      <!--            @click="handleUpdate"-->
      <!--            v-hasPermi="['business:authRecord:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--            type="danger"-->
      <!--            plain-->
      <!--            icon="Delete"-->
      <!--            :disabled="multiple"-->
      <!--            @click="handleDelete"-->
      <!--            v-hasPermi="['business:authRecord:remove']"-->
      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['business:authRecord:export']">导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="authRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="认证记录Id" align="center" prop="authRecordId" />
      <el-table-column label="认证用户Id" align="center" prop="userId" />
      <el-table-column label="认证的身份Id" align="center" prop="postId" />
      <el-table-column label="所属报部门" align="center" prop="deptId" />
      <el-table-column label="审核状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="business_works_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="申请原因" align="center" prop="applicationReason" />
      <el-table-column label="不通过原因" align="center" prop="reason" />
      <el-table-column label="认证资料" align="center" prop="authFile" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <el-button v-if="row.status === 0" link type="warning" icon="Finished" @click="handleAudit(row)">审核
          </el-button>
          <!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(row)"-->
          <!--                     v-hasPermi="['business:authRecord:edit']">修改-->
          <!--          </el-button>-->
          <!--          <el-button link type="primary" icon="Delete" @click="handleDelete(row)"-->
          <!--                     v-hasPermi="['business:authRecord:remove']">删除-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改认证记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="authRecordRef" :model="form" :rules="rules" label-width="120">
        <el-form-item label="认证用户Id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入认证用户Id" />
        </el-form-item>
        <el-form-item label="认证的身份Id" prop="postId">
          <el-input v-model="form.postId" placeholder="请输入认证的身份Id" />
        </el-form-item>
        <el-form-item label="所属报部门" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入所属报部门" />
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择审核状态">
            <el-option v-for="dict in business_works_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="不通过原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入不通过原因" />
        </el-form-item>
        <el-form-item label="申请原因" prop="applicationReason">
          <el-input v-model="form.applicationReason" placeholder="请输入申请原因" />
        </el-form-item>
        <el-form-item label="认证资料" prop="authFile">
          <file-upload v-model="form.authFile" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="title" v-model="auditOpen" width="70%" append-to-body>
      <el-form ref="authRecordRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="审核状态" prop="status">
          <el-select class="ff" v-model="form.status" placeholder="请选择审核状态">
            <el-option v-for="dict in business_works_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.status === 2" label="失败原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入失败原因" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="reviewHandler">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="AuthRecord">
import {
  addAuthRecord,
  delAuthRecord,
  getAuthRecord,
  listAuthRecord,
  review,
  updateAuthRecord
} from "@/api/business/authRecord";

const { proxy } = getCurrentInstance();
const { business_works_status } = proxy.useDict('business_works_status');

const authRecordList = ref([]);
const open = ref(false);
const auditOpen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    postId: null,
    deptId: null,
    status: null,
    reason: null,
    applicationReason: null,
    authFile: null,
  },
  rules: {
    userId: [
      { required: true, message: "认证用户Id不能为空", trigger: "blur" }
    ],
    postId: [
      { required: true, message: "认证的身份Id不能为空", trigger: "blur" }
    ],
    deptId: [
      { required: true, message: "所属报部门不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "0待审核 1审核通过 2审核不通过不能为空", trigger: "change" }
    ],
    applicationReason: [
      { required: true, message: "申请原因不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/**审核资料*/
function handleAudit(row) {
  reset();
  const _authRecordId = row.authRecordId || ids.value
  getAuthRecord(_authRecordId).then(response => {
    form.value = response.data;
    auditOpen.value = true;
    title.value = "修改认证记录";
  });
}

/** 查询认证记录列表 */
function getList() {
  loading.value = true;
  listAuthRecord(queryParams.value).then(response => {
    authRecordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  auditOpen.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    authRecordId: null,
    userId: null,
    postId: null,
    deptId: null,
    status: null,
    reason: null,
    applicationReason: null,
    authFile: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null
  };
  proxy.resetForm("authRecordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.authRecordId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加认证记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _authRecordId = row.authRecordId || ids.value
  getAuthRecord(_authRecordId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改认证记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["authRecordRef"].validate(valid => {
    if (valid) {
      if (form.value.authRecordId != null) {
        updateAuthRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          auditOpen.value = false;
          getList();
        });
      } else {
        addAuthRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          auditOpen.value = false;
          getList();
        });
      }
    }
  });
}
/*审核认证资料*/
function reviewHandler() {
  proxy.$refs["authRecordRef"].validate(valid => {
    if (valid) {
      review(form.value).then(() => {
        proxy.$modal.msgSuccess("审核成功");
        open.value = false;
        auditOpen.value = false;
        getList();
      })
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _authRecordIds = row.authRecordId || ids.value;
  proxy.$modal.confirm('是否确认删除认证记录编号为"' + _authRecordIds + '"的数据项？').then(function () {
    return delAuthRecord(_authRecordIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/authRecord/export', {
    ...queryParams.value
  }, `authRecord_${new Date().getTime()}.xlsx`)
}

getList();
</script>
